using System;
using UnityEngine;
using Cysharp.Threading.Tasks;
using Framework.Interface;
using Framework.Manager;
using Game.Hotfix;
using UnityEngine.UI;
using ZTest.Event;

public class UniTaskDemo : MonoBehaviour, IApp
{
    public Image image;
    public Transform parent;

    private Game.Hotfix.Hotfix hotfix;
    
    async void Start()
    {
        DontDestroyOnLoad(gameObject);
        // Test().ToCoroutine();
        /*
        await ResourceManager.Instance.Initialize(transform);
        GameObject obj = await ResourceManager.Instance.LoadAssetAsync<GameObject>("Assets/Res/Prefabs/Cube.prefab");
        GameObject.Instantiate(obj);

        image.sprite = await ResourceManager.Instance.LoadSpriteAsync("Assets/Res/Images/Login/DengLu_BTN_Huang.png");
        image.sprite = await ResourceManager.Instance.LoadSpriteAsync("Assets/Res/Images/Login/denglu_xuanzhong.png");

        GameObject loginGo = await ResourceManager.Instance.LoadPrefabAsync("Assets/Res/Prefabs/LoginView.prefab");
        GameObject login = GameObject.Instantiate(loginGo);
        login.transform.SetParent(parent, false);

        await NetworkManager.Instance.Initialize(transform);
        NetworkManager.Instance.OnError = (connId, error) =>
        {
            Debug.Log($"{connId} has error: {error}");
        };
        SocketMessage connMsg = await NetworkManager.Instance.AsyncConnect("192.168.0.107", 12347, SocketProtocolType.Kcp);
        Debug.Log("AA  " + connMsg.ConnectionId + " " + connMsg.SessionId);
        NetMessage.S2CLogin loginDto = await NetworkManager.Instance.Call<NetMessage.S2CLogin>(connMsg.ConnectionId, new NetMessage.C2SLogin
        {
            Openid = "Henry"
        });
        
        Debug.Log("BB  " + loginDto.Ok + " " + loginDto.Time);
        
        // UnityWebRequest request = UnityWebRequest.Get("http://192.168.0.107:10011/entryHandler.launcher?channel=windows&version=2");
        // await request.SendWebRequest();
        // Debug.Log("*****  " + request.downloadHandler.text);
        */
        
        // 测试事件
        EventsManager.Instance.Add<TestEvent>(TestEvent1);
        EventsManager.Instance.Add<TestEvent2>(TestEvent1);
        
        EventsManager.Instance.Broadcast(new TestEvent(){Name = "henry" });
        EventsManager.Instance.Broadcast(new TestEvent2(){Age = 20});
        EventsManager.Instance.Remove<TestEvent>(TestEvent1);
        EventsManager.Instance.Broadcast(new TestEvent(){Name = "henry2"});
        
        // 热更结束事件
        EventsManager.Instance.Add<HotfixEndEvent>(HotfixEnd);
        
        hotfix = new Game.Hotfix.Hotfix();
        await hotfix.Start();

        await HotfixManager.Instance.Initialize();
        
        await ResourceManager.Instance.Initialize(transform);
        GameObject obj = await ResourceManager.Instance.LoadAssetAsync<GameObject>("Assets/Res/Prefabs/Cube.prefab");
        GameObject.Instantiate(obj);
        
        image.sprite = await ResourceManager.Instance.LoadSpriteAsync("Assets/Res/Images/Login/DengLu_BTN_Huang.png");
        image.sprite = await ResourceManager.Instance.LoadSpriteAsync("Assets/Res/Images/Login/denglu_xuanzhong.png");

        GameObject loginGo = await ResourceManager.Instance.LoadPrefabAsync("Assets/Res/Prefabs/LoginView.prefab");
        GameObject login = GameObject.Instantiate(loginGo);
        login.transform.SetParent(parent, false);
    }

    private void Update()
    {
        
    }

    private void HotfixEnd(IListener listener)
    {
        Debug.Log("hotfix end, enter game pls.");
    }

    private async UniTask<int> Test()
    {
        // GameObject u = await Resources.LoadAsync<GameObject>("") as GameObject;
        await UniTask.WaitForSeconds(2);
        Debug.Log("AAA");
        await UniTask.Delay(TimeSpan.FromSeconds(1));
        Debug.Log("******");
        
        return 9;
    }

    private void TestEvent1(IListener listener)
    {
        if (listener is TestEvent)
        {
            Debug.Log("AA  " + (listener as TestEvent).Name);
        }
        else if (listener is TestEvent2)
        {
            Debug.Log("BB  " + (listener as TestEvent2).Age);
        }
    }
}
